package com.maple.common.widget.wheel

import com.maple.common.ext.clamp

/**
 * Tip:
 * Create by SeVen on 2024/3/29 14:48
 */
class TimeWheelAdapter(val times: MutableList<Int>, var unit: String? = null) : WheelAdapter() {

    init {
        if (unit == null) {
            unit = ""
        }
    }

    private fun getMaxIndex(): Int = getSize() - 1

    private fun getMinIndex(): Int = 0

    override fun getValue(position: Int): String {
        if (position >= getMinIndex() && position <= getMaxIndex()) {
            return if (times[position] < 10) {
                "0${times[position]}$unit"
            } else {
                "${times[position]}$unit"
            }
        }

        if (position <= getMaxIndex()) {
            return if (times[position] < 10) {
                "0${times[position]}$unit"
            } else {
                "${times[position]}$unit"
            }
        }
        if (position >= getMinIndex()) {
            return if (times[position] < 10) {
                "0${times[position]}$unit"
            } else {
                "${times[position]}$unit"
            }
        }
        return ""
    }

    override fun getPosition(vale: String): Int {
        return times.indexOf(vale.toInt()).clamp(getMinIndex(), getMaxIndex())
    }

    override fun getTextWithMaximumLength(): String {
        return "2024"
    }

    override fun getSize(): Int {
        return times.size
    }

}